//
// Created by antler on 2018/10/29.
//

// 触摸命令

#include "lprefix.h"

#include <stdio.h>

#include "lua.hpp"
#include "luaext.hpp"

#include "TouchUtils.h"

static int touch_down(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 3) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    int index = luaL_checkint(L, 1);
    int x = luaL_checkint(L, 2);
    int y = luaL_checkint(L, 3);

    TouchUtils::down(index, x, y);
    return 0;
}

static int touch_move(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 3) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    int index = luaL_checkint(L, 1);
    int x = luaL_checkint(L, 2);
    int y = luaL_checkint(L, 3);

    TouchUtils::move(index, x, y);
    return 0;
}

static int touch_up(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 3) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    int index = luaL_checkint(L, 1);
    int x = luaL_checkint(L, 2);
    int y = luaL_checkint(L, 3);

    TouchUtils::up(index, x, y);
    return 0;
}

static int touch_tap(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 2) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    int x = luaL_checkint(L, 1);
    int y = luaL_checkint(L, 2);

    TouchUtils::tap(x, y);
    return 0;
}

static int touch_swipe(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 4) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    int x1 = luaL_checkint(L, 1);
    int y1 = luaL_checkint(L, 2);
    int x2 = luaL_checkint(L, 3);
    int y2 = luaL_checkint(L, 4);
    int duration = luaL_optint(L, 5, -1);

    TouchUtils::swipe(x1, y1, x2, y2, duration);
    return 0;
}

static int touch_press(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 1) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    if (lua_isinteger(L, 1)) {
        int type = lua_tointeger(L, 1);

        bool longPress = false;
        if (lua_isboolean(L, 2)) {
            longPress = lua_toboolean(L, 2) ? true : false;
        }

        TouchUtils::press(type, longPress);
    } else if (lua_isstring(L, 1)) {
        const char *keyName = lua_tostring(L, 1);

        bool longPress = false;
        if (lua_isboolean(L, 2)) {
            longPress = lua_toboolean(L, 2) ? true : false;
        }

        TouchUtils::press(keyName, longPress);
    } else {
        lua_pushliteral(L, "invalid params");
        lua_error(L);
    }

    return 0;
}

static int touch_doublePress(lua_State *L) {
    int argc = lua_gettop(L);
    if (argc < 1) {
        lua_pushliteral(L, "need more params");
        lua_error(L);
    }

    if (lua_isinteger(L, 1)) {
        int type = lua_tointeger(L, 1);

        TouchUtils::doublePress(type);
    } else if (lua_isstring(L, 1)) {
        const char *keyName = lua_tostring(L, 1);

        TouchUtils::doublePress(keyName);
    } else {
        lua_pushliteral(L, "invalid params");
        lua_error(L);
    }
    return 0;
}

static int touch_captureTap(lua_State *L) {

    return 0;
}

static const luaL_Reg funcs[] = {
        {"down",        touch_down},
        {"move",        touch_move},
        {"up",          touch_up},
        {"tap",         touch_tap},
        {"swipe",       touch_swipe},
        {"press",       touch_press},
        {"doublePress", touch_doublePress},
        {"KEY_HOME",        NULL},
        {"KEY_BACK",        NULL},
        {"KEY_MENU",        NULL},
        {"KEY_POWER",       NULL},
        {"KEY_VOLUME_UP",   NULL},
        {"KEY_VOLUME_DOWN", NULL},
        {NULL,              NULL}
};

LUAMOD_API int luaopen_touch(lua_State *L) {
    luaL_register(L, "touch", funcs);

    lua_pushinteger(L, KEY_HOME);
    lua_setfield(L, -2, "KEY_HOME");

    lua_pushinteger(L, KEY_BACK);
    lua_setfield(L, -2, "KEY_BACK");

    lua_pushinteger(L, KEY_MENU);
    lua_setfield(L, -2, "KEY_MENU");

    lua_pushinteger(L, KEY_POWER);
    lua_setfield(L, -2, "KEY_POWER");

    lua_pushinteger(L, KEY_VOLUME_UP);
    lua_setfield(L, -2, "KEY_VOLUME_UP");

    lua_pushinteger(L, KEY_VOLUME_DOWN);
    lua_setfield(L, -2, "KEY_VOLUME_DOWN");
    return 0;
}